import { createRouter,  createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/login' }, 
    { path: '/login', component: () => import('@/views/login.vue') },
    { path: '/chats', component: () => import('@/views/chats.vue') }
  ]
})


// 登录访问拦截
router.beforeEach((to) => {
  const userStore = useUserStore()
  const token = localStorage.getItem('token')
  if (!token && to.path !== '/login'){
    ElMessage({ message: '请先登录', type: 'error' })
    return '/login'
  } 
})

export default router